<template>
  <div class="app-container">
    <div>
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query.queryMap"
          :query-more-json="true"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
          :clear-type="clearType"
          :slip-select-request="slipSelectRequest"
        >
        </NewPageFilter>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btnList">
          <template v-slot:filter-switchFiscalPeriod>
            <el-button @click="switchFiscalPeriod">切换会计期间</el-button>
          </template>
        </NewPageFilter>
        <div class="display-flex">
          <el-button @click="preferenceInfo.visible = true">偏好设置</el-button>
        </div>
      </div>
      <!-- 表格 -->
      <table-container
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :check-box="false"
        :handle="tableInfo.handle"
        @sortChange="sortChange"
      ></table-container>
    </div>
    <!--高级搜索-->
    <advanced-search
      v-if="advancedSearchInfo.visible"
      v-model:visible="advancedSearchInfo.visible"
      v-model:form-data="advancedSearchInfo.data"
      :field-list="tableInfo.fieldList"
      @callback="advancedSearchCallback"
    />
    <!--偏好设置-->
    <set-preference
      v-if="preferenceInfo.visible"
      v-model:visible="preferenceInfo.visible"
      :search-list="filterInfo.searchList"
      :field-list="tableInfo.fieldList"
      :customize-user-v-o="filterInfo.query.customizeUserVO"
      :group-level-show="false"
      @callback="setPreferenceCallback"
    />
  </div>
</template>

<script setup name="assetFiscalPeriod">
import NewPageFilter from "@/components/NewPageFilter/index";
import TableContainer from "@/components/TableContainer/index";
import {
  getFiscalPeriodPage,
  getCurrentFiscalPeriod,
  changeFiscalPeriod,
  materialMonthClose,
} from "@/api/core.js";
import { getCustomizeSetting, updateUserCustomize } from "@/api/customize";
const { proxy } = getCurrentInstance();

// 过滤相关配置
const filterInfo = reactive({
  sortArr: [],
  copyQueryMap: {},
  query: {
    customizeUserVO: {},
    queryMap: {
      fiscalType: {
        queryType: "EQUALS",
        queryCategory: "AND",
        value: 1,
      },
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [
    { type: "button", label: "搜索", event: search },
    { type: "button", label: "高级查询", event: advancedSearchShow },
    { type: "button", label: "重置", event: clearSearch },
  ],
  btnList: [
    {
      type: "slot",
      value: "switchFiscalPeriod",
      show: proxy.$permission("materialFiscalPeriod:switch"),
    },
  ],
});
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  selections: [],
  fieldList: [],
  handle: {
    fixed: "right",
    label: "操作",
    width: '50',
    btList: [
      {
        label: "月结",
        event: settlement,
        show: proxy.$permission("materialFiscalPeriod:month"),
        ifRender: function (row) {
          return row.monthCloseStatus === 2 && row.lastMonthCloseStatus === 1;
        },
      },
    ],
  },
});
const advancedSearchInfo = reactive({
  visible: false,
  data: {},
});
const preferenceInfo = reactive({
  visible: false,
});
const clearType = ref(true);
const slipSelectRequest = ref(true);

let obj = {
  name: {
    label: "名称",
    value: "name",
    componentType: "input",
    show: false,
    hidden: true,
  },
  statusValue: {
    label: "状态",
    value: "statusValue",
    asValue: "status",
    componentType: "select-model",
    code: "fiscal_period_status",
    show: false,
    hidden: true,
  },
  startDate: {
    label: "开始日期",
    value: "startDate",
    type: "column",
    show: false,
    hidden: true,
  },
  endDate: {
    label: "结束日期",
    value: "endDate",
    type: "column",
    show: false,
    hidden: true,
  },
};
tableInfo.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
});
filterInfo.searchList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
});
getCustomize();

function switchFiscalPeriod() {
  getCurrentFiscalPeriod(1).then((res) => {
    if (res && res.success) {
      proxy.$confirm(
        `切换会计期间后，将无法做'${res.data.name}'的耗材业务，是否确认切换?`,
        "提示",
        {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        checkSwitchFiscalPeriod();
      });
    }
  });
}
function checkSwitchFiscalPeriod() {
  changeFiscalPeriod({ fiscalType: 1 }).then((res) => {
    if (res && res.success) {
      getList();
    }
  });
}
function settlement(row) {
  materialMonthClose(row.id).then((res) => {
    if (res && res.success) {
      getList();
    }
  });
}
async function setPreferenceCallback(obj) {
  updateUserCustomize(obj.customizeUserVO).then((res) => {
    if (res && res.success) {
      getCustomize(!slipSelectRequest.value);
    }
  });
}
function advancedSearchShow() {
  advancedSearchInfo.visible = true;
}
async function advancedSearchCallback(obj) {
  
  filterInfo.query.queryMap = {};
  filterInfo.query.queryDtoList = [...obj.queryDtoList];
  advancedSearchInfo.data = obj.data;
  await getList();
  clearType.value = !clearType.value;
  filterInfo.query.queryMap = proxy.$fn.deepClone(filterInfo.copyQueryMap);
}
// 获取列表
async function getList() {
  tableInfo.refresh = Math.random();
}
// 搜索
function search() {
  // 重置分页
  
  advancedSearchInfo.data = {};
  filterInfo.query.queryDtoList = [];
  tableInfo.initCurpage = Math.random();
  tableInfo.refresh = Math.random();
}
function clearSearch() {
  clearType.value = !clearType.value;
  advancedSearchInfo.data = {};
  filterInfo.query.queryMap = proxy.$fn.deepClone(
    filterInfo.copyQueryMap
  );
  search();
}
function getCustomize(_slipSelectRequest = true) {
  
  
  let param = {
    tableKey:
      "materialFiscalPeriodOrderList-com.slip.spd.entity.vo.asset.settings.CoreFiscalPeriodVO",
  };
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data;
      filterInfo.query.customizeUserVO = data;
      tableInfo.fieldList = proxy.$fn.setFieldList(
        tableInfo.fieldList,
        data.detail
      );
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.searchList,
        data.detail
      );
      filterInfo.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.query.queryMap,
      };
      filterInfo.searchList = searchInfo.searchList;
      filterInfo.searchList.forEach((item) => {
        item.event = search;
      });
      filterInfo.copyQueryMap = proxy.$fn.deepClone(
        filterInfo.query.queryMap
      );
      slipSelectRequest.value = _slipSelectRequest;
      tableInfo.api = getFiscalPeriodPage;
      getList();
    }
  });
}
function sortChange(column) {
  
  if (filterInfo.query.customizeUserVO.head.sortType === 0) {
    let arr = proxy.$fn.sortChange(column, filterInfo.sortArr);
    filterInfo.sortArr = arr;
    filterInfo.query.sortString = arr.length ? arr.toString() : "";
    getList();
  }
}
</script>

<style scoped lang="scss">
.dialogInfo-form-title {
  width: 100%;
  padding-left: 10px;
  color: #6dc1a9;
  margin-bottom: 20px;
  span {
    color: rgb(95, 91, 91);
    font-size: 12px;
  }
}
</style>
